﻿using System;
using System.Data;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace WebLogin
{
    public partial class _Default : Page
    {

        #region 加载页面
        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack)
            {

            }
            else
            {
                FillData();
            }
            //Response.Write("<script>alert('欢迎来到内容管理系统')</script>"); *只在加载页面中出现一次（未解决） 现状：点击编辑、更新或删除按钮会重复出现
        }
        #endregion

        #region 数据传入
        private void FillData()
        {
            var sql = "select id,username,password from Users";
            var dt = DbHelper.GetDataTable(sql);
            GV1.DataSource = dt;
            GV1.DataBind();

        }
        #endregion

        #region 用户登录界面
        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {

        }
        #endregion

        #region 取消编辑操作
        //取消编辑
        protected void GV1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV1.EditIndex = -1;
            FillData();
        }
        #endregion

        #region 删除操作
        protected void GV1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            var deleteRow = GV1.Rows.Count; //获取行数
            var Id = GetCellValue(e.RowIndex, 1);
            //var content = GV1.Rows[0].Cells[0];//获取主键
            if (GV1.Rows.Count > 0)
            {

                var res = MessageBox.Show("是否删除？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (res.Equals(DialogResult.Yes))
                {
                    if (deleteRow > 0)
                    {

                        string sql = string.Format("DELETE FROM Users WHERE id = '{0}'", Id);

                        var dtDelete = DbHelper.AddOrUpdateOrDelete(sql);

                        if (dtDelete != 0)
                        {
                            MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            //填充数据
                            FillData();
                        }
                        else
                        {
                            MessageBox.Show("删除失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("取消成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }

        }
        #endregion

        #region 更新操作
        protected void GV1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var username = GetCellValue(e.RowIndex, 2);
            var password = GetCellValue(e.RowIndex, 3);

            var sql = string.Format("update Users set username = '{0}',password = '{1}'", username, password);
            DbHelper.AddOrUpdateOrDelete(sql);
            FillData();
            //Response.Write("<script language=javascript>alter('更新成功'):opener.location.reload():windows.close():</script>");


        }
        #endregion

        #region 编辑操作
        protected void GV1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV1.EditIndex = e.NewEditIndex;
            FillData();
        }
        #endregion

        #region 获取数据库行列值
        protected string GetCellValue(int rowindex, int colindex)
        {

            //获取数据库的行与列
            var control = GV1.Rows[rowindex].Cells[colindex];
            //索引从0开始遍历数据库数据，如果数据>0则进行获取，如果等于0则返回原内容
            var res = control.Controls.Count > 0 ? ((System.Web.UI.WebControls.TextBox)control.Controls[0]).Text : control.Text;
            return res;

        }
        #endregion

        #region 登录按钮
        protected void Button1_Click(object sender, EventArgs e)
        {
            //获取文本框中的值
            string username = this.TextBox1.Text;
            string password = this.TextBox2.Text;

            if (username.Equals("") || password.Equals(""))//用户名或密码为空
            {
                MessageBox.Show("用户名或密码不能为空");
            }
            else//用户名或密码不为空
            {
                //到数据库中验证
                string selectSql = string.Format("select username,password from Users where username='{0}' and password='{1}'", username, password);
                SqlHelp sqlHelper = new SqlHelp();
                var count = DbHelper.GetDataTable(selectSql);//返回符合的结果数量
                if (count.Rows.Count > 0)//如果信息>0则说明匹配成功
                {
                    MessageBox.Show("信息验证成功");
                    //页面跳转
                    Response.Redirect("http://www.baidu.com");

                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                }
            }

        }
        #endregion
    }





}




